//
// Created by YE on 2024/2/16.
//

#ifndef MATINTERPRETER_EXECUTOR_H
#define MATINTERPRETER_EXECUTOR_H


#include "instruction.h"
#include "memory.h"
#include "../common/ych_exception.h"
#include "../common/ych_error.h"
#include "../common/ych_mat.h"

#define EX_COMPUTE_ERROR 0
#define EX_STORE_ERROR 1
#define EX_POINTER_ERROR 2
typedef Instruction *PC;
typedef int IX;

typedef struct GCNode {
    struct GCNode *pre;
    Object *object;
} *GCList, GCNode;

typedef struct MatGCNode {
    struct MatGCNode *pre;
    Mat *mat;
} *MatGCList, MatGCNode;
typedef struct CallState {
    struct CallState *pre;
    Object *param_stack_start;
    Instruction *return_ip;
    MatGCList mat_gc_list;
} CallState;
typedef struct YState YState;
typedef struct YExecutor {
    YState *y_state;
    Object VM_R[YCH_VM_REGISTER_SIZE];
    Object VM_S[YCH_VM_STACK_SIZE];
    VMMemory *memory;
    PC pc;
    GCList object_gc_list;
    MatGCList mat_gc_list;
} Executor;

void initExecutor(Executor *executor, YState *y_state, PC pc, VMMemory *vmMemory);

void runExecutor(Executor *executor);

void freeExecutor(Executor *executor);

void throwExecutor(Executor *executor, int code);

void gc(Executor *executor);

#endif //MATINTERPRETER_EXECUTOR_H
